package code.tools.commponents
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	/**
	 * drawText(text,color);
	 * 绘制autoSize文字
	 * setSize(w,h);
	 * 可设置对齐范围
	 * @author liangqian
	 */
	public class TextBitmap extends Bitmap implements ISelfDisposableDisplayObject
	{
		private var _width:int;

		private var _height:int;

		protected var _x:int;
		protected var _y:int;

		private var _regPointAt:int;

		private var _color:int = Style.COLOR_LABEL_TEXT;

		private var _text:String;

		private var _fontSize:int ;

		/**
		 *
		 * @param parent
		 * @param xpos
		 * @param ypos
		 * @param regPointAt 对齐方式 （默认为左上角）
		 * @param color 文本颜色 默认 label Text color
		 */
		public function TextBitmap(parent:DisplayObjectContainer = null , xpos:Number = 0 , ypos:Number = 0 ,
								    regPointAt:int = 0,color:int=0xC3946A)
		{
			super();
			if (parent != null)
				parent.addChild(this);
			x = xpos;
			y = ypos;
			_regPointAt = regPointAt;
			_color = color;
//			drawText(text , color);
			addEventListener(Event.REMOVED_FROM_STAGE , dispose); /* 当从屏幕上被移除时自动销毁 */
		}

		/**
		 * BitmapData width
		 */
		public function get bw():int
		{
			if (bitmapData)
				return bitmapData.width;
			return 0;
		}

		/**
		 * BitmapData height
		 */
		public function get bh():int
		{
			if (bitmapData)
				return bitmapData.height;
			return 0;
		}

		/**
		 * 是否取消这个位图的自我销毁功能
		 */
		public function set keepAlive(value:Boolean):void
		{
			if (value)
				removeEventListener(Event.REMOVED_FROM_STAGE , dispose);
			else
				addEventListener(Event.REMOVED_FROM_STAGE , dispose);
		}

		/**
		 * dispose the current Sgfbitmap instance
		 */
		public function dispose(... rest):void
		{
			bitmapData = null;
			_text = null;
			removeEventListener(Event.ENTER_FRAME , onInvalidate);
			removeEventListener(Event.REMOVED_FROM_STAGE , dispose);
		}

		/**
		 * 缓步调整
		 */
		public function onInvalidate(... rest):void
		{
			removeEventListener(Event.ENTER_FRAME , onInvalidate);
			super.x = Alignment.CalcXRegedValue(bw , _regPointAt , Alignment.CalcXAlignedValue(_width , _regPointAt , _x , _x))
			super.y = Alignment.CalcYRegedValue(bh , _regPointAt , Alignment.CalcYAlignedValue(_height , _regPointAt , _y , _y));
		}

		protected function invalidate(... rest):void
		{
			if (bitmapData == null)
				return;
			addEventListener(Event.ENTER_FRAME , onInvalidate);
		}

		/**
		 * 位图的注册点方式
		 */
		public function get regPointAt():int
		{
			return _regPointAt;
		}

		/**
		 * @private
		 */
		public function set regPointAt(value:int):void
		{
			_regPointAt = value;
			invalidate();
		}
		///////////////////////////////////
		// draw text methods
		///////////////////////////////////
		private function drawText(t:String , c:int = -1,fontSize:int=12):void
		{
			if (c < 0)
				c = _color;
			if (t == _text && c == _color&&_fontSize==fontSize)
				return;
			_text = t;
			_color = c;
			_fontSize = fontSize;
			bitmapData = TextBitmapData.textToBitmap(_text , _color,_fontSize);
			onInvalidate();
		}
		/**
		 * 目前按钮仍使用label样式
		 * 之后可能会做更改
		 */
		public function drawButtonText(t:String,c:int=-1):BitmapData
		{
			drawText(t,c,14);
			return bitmapData;
		}
		public function drawLabelText(t:String,c:int=-1):void
		{
			drawText(t,c);
		}
		public function drawBigLabelText(t:String,c:int=-1):void
		{
			drawText(t,c,14);
		}
		public function drawHtmlText(t:String,c:int=0xffffff,fontSize:int = 14):void
		{
			bitmapData = TextBitmapData.htmlToBitmap(t , c,fontSize);
			onInvalidate();
		}
//		public function drawEmbedFontText(t:int,c:uint,fontSize:int=14):void
//		{
//			bitmapData = TextBitmapData.EmbedFontToBitmap(t,c,fontSize);
//			onInvalidate();
//		}
		///////////////////////////////////
		// get/set
		///////////////////////////////////
		public function get text():String
		{
			return _text;
		}

		public function setSize(w:int , h:int):void
		{
			_width = w;
			_height = h;
			onInvalidate();
		}

		override public function get x():Number
		{
			return _x;
		}

		/**
		 * 设置x坐标
		 */
		override public function set x(xpos:Number):void
		{
			_x = Math.round(xpos);
			invalidate();
		}

		override public function get y():Number
		{
			return _y;
		}

		/**
		 * 设置y坐标
		 */
		override public function set y(ypos:Number):void
		{
			_y = Math.round(ypos);
			invalidate();
		}
	}
}
